package com.cyh.pangu.offer;

import java.util.Deque;
import java.util.LinkedList;

public class Offer59 {

    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums.length == 0 || k == 0) {
            return new int[0];
        }
        Deque<Integer> deque = new LinkedList<>();
        int[] res = new int[nums.length - k + 1];
        for (int i = 0; i < k; i++) {
            while (!deque.isEmpty() && deque.peekLast() < nums[i]) {
                deque.removeLast();
            }
            deque.addLast(nums[i]);
        }
        res[0] = deque.peekFirst();
        for (int i = k; i < nums.length; i++) {
            if (deque.peekFirst() == nums[i-k]) {
                deque.removeFirst();
            }

            while (!deque.isEmpty() && deque.peekLast() < nums[i]) {
                deque.removeLast();
            }
            deque.addLast(nums[i]);
            res[i-k+1] = deque.peekFirst();
        }
            return res;
    }

    public static void main(String[] args) {
        int[] nums = new int[] {3, 2, 1, -1, -2};
        int k = 3;
        Offer59 offer59 = new Offer59();
        offer59.maxSlidingWindow(nums, k);
    }

}
